package com.zjj.lbw.microservice.nacos.consumer.nacosconsumer.ribbon;

import com.zjj.lbw.common.bean.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;


/**
 * @author zhanglei.zjj
 * @description 使用自己Mock的Ribbon实现来做负载均衡的MVC接口
 * @date 2023/7/5 21:29
 */
@RestController
@RequestMapping("/ribbon/user")
@Slf4j
public class RibbonUserController {
    // 我们自己注入的RestTemplate 加了@LoadBalanced注解，要先暂时去掉
    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private RibbonMock ribbonMock;

    @RequestMapping(value = "/findOrderByUserId/{id}")
    public R findOrderByUserId(@PathVariable("id") Integer id) {
        //模拟ribbon实现
        String url = ribbonMock.getUri("nacos-provider")+"/order/findOrderByUserId/"+id;
        R result = restTemplate.getForObject(url,R.class);
        return result;
    }
}

